﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MigraDoc.DocumentObjectModel;
using Payroll.BusinessLogic.CustomerManager;
using Payroll.BusinessLogic.Utils;
using Payroll.Common.Utils;
using PayrollHR.Web;
using Payroll.BusinessLogic.Company;

namespace PayrollHR.Administration.Modules
{
    public partial class CompanyAdd : BasePayrollAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                pnlYesNo.Visible = false;
                    BindCompany();
                
            }
        }
        protected void BindCompany()
        {
            
            var company = CompanyServices.GetCompanyById(this.MyCompanyId);// Convert.ToInt32(Request.QueryString["MyCompanyID"])); //this.CompanyServices.g.GetCustomerById(this.CustomerID);
            if (CommonHelper.IsNull(company))
                return;
            ctrlCompanyInfo.BindCompany(company);
        }
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                lblCompany.Visible = false;
                try
                {
                    Payroll.BusinessLogic.Company.MyCompany company = ctrlCompanyInfo.GetCompany();
                    if (company.MyCompanyID == 0)
                    {
                        var cust = CompanyServices.GetCompanyByName(company.Name);
                        if (CommonHelper.IsNull(cust))
                        {
                            if (!CommonHelper.IsValidEmail(company.Receive) || !CommonHelper.IsValidEmail(company.EmailSend) || !CommonHelper.IsValidEmail(company.EmailSender))
                            {
                                lblCompany.Text = "Email không hợp lệ";
                                lblCompany.Visible = true;
                            }
                            else
                            {
                                SaveButton.Enabled = false;
                                SaveAndStayButton.Enabled = false;
                                this.Save();
                                Response.Redirect("MyCompany.aspx");
                            }
                        }
                        else
                        {
                            lblCompany.Visible = true;
                            //lbMessage.Text = String.Format("Khách hàng tên {0} đã có, bạn tiếp tục lưu nhấn button kế bên.", company.Name);
                            pnlYesNo.Visible = true;
                        }
                    }
                    else
                    {
                        this.Continue_Click(sender, e);
                    }
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }
        protected Payroll.BusinessLogic.Company.MyCompany Save()
        {
            Payroll.BusinessLogic.Company.MyCompany company = ctrlCompanyInfo.GetCompany();
            if (!CommonHelper.IsValidEmail(company.Receive) || !CommonHelper.IsValidEmail(company.EmailSend) || !CommonHelper.IsValidEmail(company.EmailSender))
            {
                lblCompany.Text = "Email không hợp lệ";
                lblCompany.Visible = true;
                Response.Redirect(String.Format("CompanyAdd.aspx?MyCompanyID={0}", company.MyCompanyID));
            }
            else
            {
                CompanyServices.Save(company);
                lblCompany.Visible = false;
            }

            return company;
        }
        protected void SaveAndStayButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                lblCompany.Visible = false;
                try
                {
                    Payroll.BusinessLogic.Company.MyCompany company = ctrlCompanyInfo.GetCompany();
                    if (company.MyCompanyID == 0)
                    {
                        var cust = CompanyServices.GetCompanyByName(company.Name);
                        if (CommonHelper.IsNull(cust))
                        {
                            if (!CommonHelper.IsValidEmail(company.Receive) || !CommonHelper.IsValidEmail(company.EmailSend) || !CommonHelper.IsValidEmail(company.EmailSender))
                            {
                                lblCompany.Text = "Email không hợp lệ";
                                lblCompany.Visible = true;
                            }
                            else
                            {
                                SaveButton.Enabled = false;
                                SaveAndStayButton.Enabled = false;
                                this.Save();
                                Response.Redirect(String.Format("CompanyAdd.aspx?MyCompanyID={0}", cust.MyCompanyID));
                            }
                        }
                        else
                        {
                            lblCompany.Visible = true;
                           // lbMessage.Text = String.Format("Khách hàng tên {0} đã có, bạn tiếp tục lưu nhấn button kế bên.", company.Name);
                            pnlYesNo.Visible = true;
                        }
                    }
                    else
                    {
                        btnSaveAndStay_Click(sender, e);
                    }
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
            
        }
        public int MyCompanyId
        {
            get
            {
                return CommonHelper.QueryStringInt("MyCompanyID");
            }
        }
        protected void Cancel_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect(String.Format("CompanyAdd.aspx?MyCompanyID={0}", 0));
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        protected void Continue_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    SaveButton.Enabled = false;
                    SaveAndStayButton.Enabled = false;
                    this.Save();
                   Response.Redirect("MyCompany.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }  
        }

        protected void btnSaveAndStay_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    SaveButton.Enabled = false;
                    SaveAndStayButton.Enabled = false;
                    Payroll.BusinessLogic.Company.MyCompany company= Save();
                    Response.Redirect(String.Format("CompanyAdd.aspx?MyCompanyID={0}", company.MyCompanyID));
                   // Response.Redirect(String.Format("CustomerAdd.aspx?CustomerId={0}", customer.CustomerId));
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }

 
    }
}